//import pathToRegexp from 'path-to-regexp';
import { getClasslistApi, getGoodsClassApi } from './api';
import { Toast } from 'antd-mobile';

export default {
  namespace: 'goodsclass',
  state: {
    classList: [],
    goodsList: {},
  },
  reducers: {
    save(state, { data }) {
      return { ...state, ...data };
    },
  },
  effects: {
    *getClasslistEFF({ }, { put, call }) {
      Toast.loading();
      const result = yield call(getClasslistApi);
      if (result.code == 200) {
        Toast.hide();
        yield put({ type: 'save', data: { classList: result.ret.list || [] } });
        // if (result.ret && result.ret.list.length > 0) {
        //   yield put({ type: 'getGoodsClassEFF', advid: result.ret.list[0].gcAdvid, pId: result.ret.list[0].gcId });
        // }
      } else {

      }
    },
    *getGoodsClassEFF({ advid, pId }, { put, call }) {
      const result = yield call(getGoodsClassApi, { advid, pId });
      if (result.code == 200) {
        yield put({ type: 'save', data: { goodsList: result.ret.list[0], selectId: `${advid}_${pId}` } });
      } else {

      }
    },
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query }) => {
        // console.log(pathname)
        if (pathname == '/goodsClass') {
          dispatch({ type: 'getClasslistEFF' });}
      });
    },
  },
};
